﻿/*
 * Last review by   : Yaser
 * Last review date : JUL-25-09  
 */
using ERP.Framework.Components.Utilities.TracingHelper;

namespace ERP.Framework.Components.ConfigurationHelper
{
    /// <summary>
    /// Configuration Reader
    /// </summary>
    public sealed class ConfigurationManager
    {
        #region Constructors

        /// <summary>
        /// Initializes the <see cref="ConfigurationManager"/> class.
        /// </summary>
        static ConfigurationManager()
        {
            TraceUtil.Write(TRACING_CATEGORY, "ConfigurationManager : Initializing");
            if (ConfigurationSettings.UseExternalConfigFile)
            {
                m_configFile = ConfiguratoinFileType.ExternalConfig;
                m_configFilePath = ConfigurationSettings.ExternalConfigurationFilePath;
                TraceUtil.Write(TRACING_CATEGORY, string.Format("ConfigurationManager : Using external config file : {0}", m_configFilePath));
            }
            else
                TraceUtil.Write(TRACING_CATEGORY, "ConfigurationManager : Using default config file");
            TraceUtil.Write(TRACING_CATEGORY, "ConfigurationManager : End");
        }

        #endregion

        #region Members

        private static ConfiguratoinFileType m_configFile = ConfiguratoinFileType.LocalConfig;
        private static string m_configFilePath = string.Empty;
        private const string TRACING_CATEGORY = "ERP.Framework.Components.ConfigurationHelper.ConfigurationManager";

        #endregion

        #region Properties

        /// <summary>
        /// Gets the connection settings.
        /// </summary>
        /// <value>The connection settings.</value>
        public static CustomSettings.DatabaseConfigurationSettings ConnectionSettings
        {
            get
            {
                TraceUtil.Write(TRACING_CATEGORY, "ConfigurationManager : ConnectionSettings");
                return CustomSettings.DatabaseConfigurationSettings.Settings;
            }
        }

        /// <summary>
        /// Gets the configuration settings.
        /// </summary>
        /// <value>The configuration settings.</value>
        public static CustomSettings.ConfigurationSettings ConfigurationSettings
        {
            get
            {
                TraceUtil.Write(TRACING_CATEGORY, "ConfigurationManager : ConfigurationSettings");
                return CustomSettings.ConfigurationSettings.Settings;
            }
        }

        /// <summary>
        /// Gets the sorting settings.
        /// </summary>
        /// <value>The sorting settings.</value>
        public static CustomSettings.SortingSettings SortingSettings
        {
            get
            {
                TraceUtil.Write(TRACING_CATEGORY, "ConfigurationManager : SortingSettings");
                return CustomSettings.SortingSettings.Settings;
            }
        }

        /// <summary>
        /// Gets the user interface settings.
        /// </summary>
        /// <value>The user interface settings.</value>
        public static CustomSettings.UISettings UserInterfaceSettings
        {
            get
            {
                TraceUtil.Write(TRACING_CATEGORY, "ConfigurationManager : UserInterfaceSettings");
                return CustomSettings.UISettings.Settings;
            }
        }

        /// <summary>
        /// Gets the exception handler settings.
        /// </summary>
        /// <value>The exception handler settings.</value>
        public static CustomSettings.ExceptionHandlerSettings ExceptionHandlerSettings
        {
            get
            {
                TraceUtil.Write(TRACING_CATEGORY, "ConfigurationManager : ExceptionHandlerSettings");
                return CustomSettings.ExceptionHandlerSettings.Settings;
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Gets the value.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <returns></returns>
        public static string GetValue(string key)
        {
            TraceUtil.Write(TRACING_CATEGORY, "GetValue : Start");
            TraceUtil.Write(TRACING_CATEGORY, string.Format("GetValue : Key:{0}", key));
            string m_value = string.Empty;
            switch (m_configFile)
            {
                case ConfiguratoinFileType.LocalConfig:
                    m_value = System.Configuration.ConfigurationManager.AppSettings[key];
                    break;
                case ConfiguratoinFileType.ExternalConfig:
                    //TODO --> implement external config code
                    break;
            }
            TraceUtil.Write(TRACING_CATEGORY, "GetValue : End");
            return m_value;
        }

        #endregion
    }
}